package com.ctshk.rpc.order.goods.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ctshk.rpc.order.goods.entity.ShoppingCart;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车 Mapper 接口
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-19
 */
public interface ShoppingCartMapper extends BaseMapper<ShoppingCart> {

    @Select({
            "<script>",
            "SELECT",
            "t.id, t.type, t.source_id, t.source_name, t.commodity_id, t.commodity_name, t.commodity_picture_url, t.commodity_price, t.commodity_number, t.number, t.user_id, t.commodity_cost_attribute_id, t.commodity_cost_attribute_name, t.commodity_status, t.gmt_create, t.create_id, t.gmt_modified, t.modified_id, t.is_deleted ",
            "FROM `ctshk-goods-order`.t_shopping_cart t ",
            "WHERE t.is_deleted = 0 ",
            "<if test='userId != null'> AND (t.user_id = #{userId} OR t.create_id = #{userId}) </if> ",
            "</script>"
    })
    List<ShoppingCart> findShoppingCartByUserId(@Param("userId")Long userId);

    @Select({
            "<script>",
            "SELECT",
            "t.id, t.type, t.source_id, t.source_name, t.commodity_id, t.commodity_name, t.commodity_picture_url, t.commodity_price, t.commodity_number, t.number, t.user_id, t.commodity_cost_attribute_id, t.commodity_cost_attribute_name, t.commodity_status, t.gmt_create, t.create_id, t.gmt_modified, t.modified_id, t.is_deleted ",
            "FROM `ctshk-goods-order`.t_shopping_cart t ",
            "WHERE t.is_deleted = 0 ",
            "<if test='userId != null'> AND (t.user_id = #{userId} OR t.create_id = #{userId}) </if> ",
            "<if test='platformCode != null'> AND (t.platform_code = #{platformCode}) </if>",
            "<if test='gmtModified != null'> AND (t.gmt_modified &gt;= #{gmtModified}) </if>",
            "</script>"
    })
    List<ShoppingCart> findPcShoppingCartByUserId(@Param("userId")Long userId,@Param("platformCode") Integer code,@Param("gmtModified") LocalDateTime gmtModified);

    @Update({
            "UPDATE `ctshk-goods-order`.t_shopping_cart  SET type=#{type}, source_id=#{sourceId}, source_name=#{sourceName}, commodity_id=#{commodityId}, commodity_name=#{commodityName}, commodity_picture_url=#{commodityPictureUrl}, commodity_price=#{commodityPrice}, commodity_number=#{commodityNumber}, number=#{number}, user_id=#{userId}, commodity_cost_attribute_id=#{commodityCostAttributeId}, commodity_cost_attribute_name=#{commodityCostAttributeName}, commodity_status=#{commodityStatus}, create_id=#{createId},  is_deleted=#{isDeleted},  gmt_create=#{gmtCreate}, gmt_modified=#{gmtModified}  WHERE id=#{id}"
    })
    void updateCartById(ShoppingCart dbCart);

    @Insert({
            "<script>" +
            "INSERT INTO `ctshk-goods-order`.t_shopping_cart  " +
                    "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
                        "<if test=\"id != null\"> id, </if>" +
                        "<if test=\"commodityId != null\"> commodity_id, </if>" +
                        "<if test=\"commodityName != null\"> commodity_name, </if>" +
                        "<if test=\"commodityStatus != null\"> commodity_status, </if>" +
                    "<if test=\"commodityPictureUrl != null\"> commodity_picture_url, </if>" +
                    "<if test=\"commodityPrice != null\"> commodity_price, </if>" +
                    "<if test=\"sourceId != null\"> source_id, </if>" +
                    "<if test=\"sourceName != null\"> source_name, </if>" +
                    "<if test=\"type != null\"> type, </if>" +
                    "<if test=\"number != null\"> number, </if>" +
                    "<if test=\"userId != null\"> user_id, </if>" +
                    "<if test=\"commodityCostAttributeId != null\"> commodity_cost_attribute_id, </if>" +
                    "<if test=\"commodityCostAttributeName != null\"> commodity_cost_attribute_name, </if>" +
                    "<if test=\"commodityNumber != null\"> commodity_number, </if>" +
                        "<if test=\"createId != null\"> create_id, </if>" +
                        "<if test=\"modifiedId != null\"> modified_id, </if>" +
                        "<if test=\"isDeleted != null\"> is_deleted, </if>" +
                        "<if test=\"platformCode != null\"> platform_code, </if>" +
                        "<if test=\"gmtCreate != null\"> gmt_create, </if>" +
                        "<if test=\"gmtModified != null\"> gmt_modified, </if>" +
                    "</trim>" +
                    "VALUES  " +
                    "<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
                        "<if test=\"id != null\"> #{id} ,</if>" +
                        "<if test=\"commodityId != null\"> #{commodityId}, </if>" +
                        "<if test=\"commodityName != null\"> #{commodityName}, </if>" +
                        "<if test=\"commodityStatus != null\"> #{commodityStatus}, </if>" +
                    "<if test=\"commodityPictureUrl != null\"> #{commodityPictureUrl}, </if>" +
                    "<if test=\"commodityPrice != null\"> #{commodityPrice}, </if>" +
                    "<if test=\"sourceId != null\"> #{sourceId}, </if>" +
                    "<if test=\"sourceName != null\"> #{sourceName}, </if>" +
                    "<if test=\"type != null\"> #{type}, </if>" +
                    "<if test=\"number != null\"> #{number}, </if>" +
                    "<if test=\"userId != null\"> #{userId}, </if>" +
                    "<if test=\"commodityCostAttributeId != null\"> #{commodityCostAttributeId}, </if>" +
                    "<if test=\"commodityCostAttributeName != null\"> #{commodityCostAttributeName}, </if>" +
                    "<if test=\"commodityNumber != null\"> #{commodityNumber}, </if>" +
                        "<if test=\"createId != null\"> #{createId}, </if>" +
                        "<if test=\"modifiedId != null\"> #{modifiedId}, </if>" +
                        "<if test=\"isDeleted != null\"> #{isDeleted}, </if>" +
                        "<if test=\"platformCode != null\"> #{platformCode}, </if>" +
                        "<if test=\"gmtCreate != null\"> #{gmtCreate}, </if>" +
                        "<if test=\"gmtModified != null\"> #{gmtModified}, </if>" +
                    "</trim>"+
            "</script>"
    })
    void insertCart(ShoppingCart shoppingCart);

    /**
     * 通过id更新购物车
     * @param shoppingCart
     */
    @Update({
            "UPDATE `ctshk-goods-order`.t_shopping_cart  SET number=#{number}, gmt_modified=#{gmtModified}, modified_id=#{modifiedId}  WHERE id=#{id}"
    })
    void updateCartInfoById(ShoppingCart shoppingCart);

    /**
     * 批量删除
     * @param ids
     */
    @Delete({
            "<script>" +
                    "delete from `ctshk-goods-order`.t_shopping_cart where id in\n" +
                    "\n" +
                    "        <foreach collection=\"list\" item=\"id\" open=\"(\" close=\")\" separator=\",\">\n" +
                    "            #{id}\n" +
                    "        </foreach>\n" +
                    "</script>"
    })
    void deleteCartByIds(@Param("list") List<Long> ids);

    /**
     * 根据商品id和属性id查询购物车商品信息
     * @param commodityId 商品id
     * @param commodityCostAttributeId 商品属性id
     * @param userId
     * @return
     */
    @Select({
            "SELECT * FROM `ctshk-goods-order`.t_shopping_cart WHERE commodity_id = #{commodityId} AND commodity_cost_attribute_id = #{commodityCostAttributeId} AND user_id = #{userId}"
    })
    List<ShoppingCart> selectByCommodityIdAndCommodityCostAttributeId(@Param("commodityId") Long commodityId,@Param("commodityCostAttributeId") Long commodityCostAttributeId,@Param("userId") Long userId);

    /**
     * 根据商品id和属性id查询购物车商品信息
     * @param commodityId 商品id
     * @param commodityCostAttributeId 商品属性id
     * @param userId
     * @return
     */
    @Select({
            "SELECT * FROM `ctshk-goods-order`.t_shopping_cart WHERE commodity_id = #{commodityId} AND commodity_cost_attribute_id is null AND user_id = #{userId}"
    })
    List<ShoppingCart> selectByCommodityIdAndUserId(@Param("commodityId") Long commodityId,@Param("userId") Long userId);
}
